package com.jxc.wiki.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxc.wiki.entity.Ebook;
import com.jxc.wiki.entity.EbookExample;
import com.jxc.wiki.mapper.EbookMapper;
import com.jxc.wiki.request.EbookQueryParamReq;
import com.jxc.wiki.request.EbookSaveParamReq;
import com.jxc.wiki.response.EbookQueryParamResp;
import com.jxc.wiki.response.PageParamResp;
import com.jxc.wiki.service.EbookService;
import com.jxc.wiki.util.CopyClassOrListUtil;
import com.jxc.wiki.util.SnowFlakeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EbookServiceImpl implements EbookService {

    private static final Logger LOG = LoggerFactory.getLogger(EbookServiceImpl.class);

    @Resource
    private EbookMapper mapper;

    @Resource
    private SnowFlakeUtil snowFlakeUtil;

    public PageParamResp<EbookQueryParamResp> list(EbookQueryParamReq req){


        // 实现输入名字查询电子书的功能，用到了模糊查询
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        // 通过判断请求的name参数是否为空（通过输入名字查询某本电子书）,不为空则使用模糊查询，没有参数的话返回所有电子书列表
        if (!ObjectUtils.isEmpty(req.getName())){
            criteria.andNameLike("%" + req.getName() + "%");
        }
        // 通过判断请求的categoryId2参数是否为空（通过输入二级分类id查询某本电子书）,不为空则使用模糊查询，没有参数的话返回所有电子书列表
        if (!ObjectUtils.isEmpty(req.getCategoryId2())){
            criteria.andCategory2IdEqualTo(req.getCategoryId2());
        }
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Ebook> ebookList = mapper.selectByExample(ebookExample);
        // 打印分页情况日志
        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());
//        // 把类型为Ebook的集合的值拷贝到类型为EbookResp的集合里
//        List<EbookResp> ebookRespList = new ArrayList<>();
//        for (Ebook ebook : ebookList) {
//            EbookResp ebookResp = new EbookResp();
//            // BeanUtils工具类，可以把ebook的值快速拷贝给ebookResp
//            BeanUtils.copyProperties(ebook,ebookResp);
//            ebookRespList.add(ebookResp);
//        }

        // 自定义Copy工具类(运用了反射的知识)快速拷贝
        List<EbookQueryParamResp> ebookRespList = CopyClassOrListUtil.copyList(ebookList, EbookQueryParamResp.class);

        PageParamResp<EbookQueryParamResp> pageParamResp = new PageParamResp<>();
        pageParamResp.setTotal(pageInfo.getTotal());
        pageParamResp.setList(ebookRespList);
        return pageParamResp;
    }

    // 保存功能,通过插入一条新的数据或者更新一条新的数据实现
    public void save(EbookSaveParamReq req){

        Ebook ebook = CopyClassOrListUtil.copy(req,Ebook.class);
        if (ObjectUtils.isEmpty(req.getId())){

            // 如果id的值为空，则为新增数据,用雪花算法生成id，并设置为ebook的id
            ebook.setId(snowFlakeUtil.nextId());
            mapper.insert(ebook);
        }else {
            // 如果id的值不为空，则为修改数据
            mapper.updateByPrimaryKey(ebook);
        }
    }

    public void delete(Long id){
        mapper.deleteByPrimaryKey(id);
    }
}
